package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.admin.entity.AdChannel;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dtos.PageResult;
import com.heima.common.util.BeanHelper;
import com.heima.model.admin.dtos.ChannelDto;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 频道信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2022-08-07
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    @Override
    public PageResult<AdChannel> findByPage(ChannelDto dto) {

//      分页条件
        IPage<AdChannel>page=new Page<>(dto.getPage(),dto.getSize());
        //检索条件
        LambdaQueryWrapper<AdChannel>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(dto.getName()),AdChannel::getName,dto.getName());
        //分页查询
        IPage<AdChannel>adChannelIPage=page(page,queryWrapper);
        //判断错误
        if (CollectionUtils.isEmpty(adChannelIPage.getRecords())){
            return null;
        }
        //返回结果
        PageResult<AdChannel>result=new PageResult<>(
                dto.getPage(),dto.getSize(),adChannelIPage.getTotal(),adChannelIPage.getRecords());
        return result;
    }
}
